package com.hengxinguotong.zhihuichengjian.ui.tower.data;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/26
 */

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.Tower;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.tower.OnFragmentInteractionListener;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/21
 *
 * 塔吊数据
 */

public class TowerDataDetailActivity extends BaseActivity implements View.OnClickListener, OnFragmentInteractionListener {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.tower_img)
    ImageView towerImg;
    @BindView(R.id.tower_hammer_container)
    RelativeLayout towerHammerContainer;
    @BindView(R.id.tower_hammer_line)
    View towerHammerLine;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private static final float TOWER_HEIGHT = 60f;//暂时假定塔吊高度60m
    private static final float TOWER_EXTENT = 60f;//暂时假定塔吊幅度60m
    private float imageHeight;//塔吊图片幅度
    private float imageExtent;//塔吊图片幅度

    private Tower tower;
    private List<String> mTitleDataList;
    private List<BaseFragment> fragments;
    private DeviceListPagerAdapter deviceListPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tower_data_detail);
        ButterKnife.bind(this);
        tower = (Tower) getIntent().getSerializableExtra("tower");
        initView();
    }

    private void initView() {
        titleTv.setText(tower.getName());

        ViewTreeObserver vto = towerImg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                towerImg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                imageExtent = towerImg.getWidth();
                imageHeight = towerImg.getHeight();
                int leftMargin = (int) ((225f/536f)*imageExtent);
                int topMargin = (int) ((98f/545f)*imageHeight);
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) towerHammerContainer.getLayoutParams();
                layoutParams.setMargins(leftMargin,topMargin, 0, 0);
                towerHammerContainer.setLayoutParams(layoutParams);

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) towerHammerLine.getLayoutParams();
                params.height = (int) ((403f/708f)*imageHeight);
                towerHammerLine.setLayoutParams(params);
            }
        });

        initViewPager();
    }

    private void initViewPager() {
        viewPager.setOffscreenPageLimit(6);
        fragments = new ArrayList<BaseFragment>();
        fragments.add(TowerDataLiveFragment.newInstance(tower.getId()));
        fragments.add(TowerDataViolationFragment.newInstance(tower.getId()));
        fragments.add(TowerDataWarningFragment.newInstance(tower.getId()));
        fragments.add(TowerDataHistoryFragment.newInstance(tower.getId()));
        fragments.add(TowerDataLiftRecycleFragment.newInstance(tower.getId()));
        fragments.add(TowerDataStatisticsFragment.newInstance(tower.getId()));
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(deviceListPagerAdapter);


        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("实时数据");
        mTitleDataList.add("违章数据");
        mTitleDataList.add("预警数据");
        mTitleDataList.add("历史数据");
        mTitleDataList.add("吊装循环");
        mTitleDataList.add("统计分析");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_f288ae5));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_f288ae5));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(TowerDataDetailActivity.this, 20);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(1.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
    }

    @OnClick({R.id.back_tv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_tv:
                finish();
                break;
        }
    }

    @Override
    public void onFragmentInteraction(int index, int count) {

    }

    @Override
    public void onUpdateTower(String heightStr, String extentStr) {
        if(!Utils.isEmpty(extentStr)){
            float extent = Float.parseFloat(extentStr);
            if(extent > TOWER_EXTENT){
                extent = TOWER_EXTENT;
            }
            int leftMargin = (int)((extent/TOWER_EXTENT)*imageExtent*(215f/708f));
            TranslateAnimation animation = new TranslateAnimation(0,leftMargin,0,0);
            long duration = (long) (extent/100 * 1000);
            animation.setDuration(duration);
            animation.setFillAfter(true);
            towerHammerContainer.startAnimation(animation);
        }

        if(!Utils.isEmpty(heightStr)){
            float high = Float.parseFloat(heightStr);
            if(high > TOWER_HEIGHT){
                high = TOWER_HEIGHT;
            }
            int height = (int)((1f - (high/TOWER_HEIGHT))*imageHeight*(403f/708f));
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) towerHammerLine.getLayoutParams();
            layoutParams.height = height;
            towerHammerLine.setLayoutParams(layoutParams);
        }
    }
}
